package com.star.percalservice.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.star.percalservice.mapper.PatentMapper;
import com.star.percalservice.mapper.TeacherMapper;
import com.star.percalservice.mapper.UserMapper;
import com.star.percalservice.model.dto.PatentDTO;
import com.star.percalservice.model.entity.Patent;
import com.star.percalservice.model.entity.Teacher;
import com.star.percalservice.model.entity.User;
import com.star.percalservice.model.vo.PatentVO;
import com.star.percalservice.service.PatentService;
import com.star.percalservice.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author star
 * @version 1.0
 * @Description:
 * @DateTime 2025/4/12 2:38
 */

@Service
public class PatentServiceImpl implements PatentService {


    @Autowired
    private PatentMapper patentMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public R list(PatentDTO dto) {
        List<Patent> patents = patentMapper.selectPatentList(dto);
        List<PatentVO> patentsVO = new ArrayList<>();
        patents.forEach(patent -> {
            PatentVO patentVO = new PatentVO();
            patentVO.setPatent(patent);
            Teacher teacher = teacherMapper.selectById(patent.getTeacherId());
            User user = userMapper.selectById(teacher.getUserId());
            patentVO.setTeacherName(user.getRealName());
            patentVO.setPatent(patent);
            patentsVO.add(patentVO);
        });
        return R.ok("查询成功", patentsVO);
    }

    @Override
    public R add(Patent patent) {
        QueryWrapper<Patent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("authorization_number", patent.getAuthorizationDate());
        if (ObjectUtil.isNotNull(patentMapper.selectOne(queryWrapper))) {
            return R.error("专利号已存在");
        }
        return patentMapper.insert(patent) > 0 ? R.ok("添加成功") : R.error("添加失败");
    }

    @Override
    public R update(Patent patent) {
        return patentMapper.updateById(patent) > 0 ? R.ok("修改成功") : R.error("修改失败");
    }

    @Override
    public R delete(Integer id) {
        return patentMapper.deleteById(id) > 0 ? R.ok("删除成功") : R.error("删除失败");
    }

    @Override
    public R changeStatus(Integer id, String status) {
        // 状态有：pending 待审核，approved 已通过，rejected 已驳回
        // 如果原状态为已驳回，则只能改为待审核
        // 如果原状态为待审核，则只能改为已通过或已驳回
        // 如果原状态为已通过， 则不可修改
        Patent patent = patentMapper.selectById(id);
        if (ObjectUtil.isNull(patent)) {
            return R.error("专利不存在");
        }
        if ("rejected".equals(patent.getStatus())) {
            if ("pending".equals(status)) {
                patent.setStatus(status);
            } else {
                return R.error("已驳回专利只能改为待审核");
            }
        } else if ("pending".equals(patent.getStatus())) {
            if ("approved".equals(status) || "rejected".equals(status)) {
                patent.setStatus(status);
            } else {
                return R.error("待审核专利只能改为已通过或已驳回");
            }
        } else if ("approved".equals(patent.getStatus())){
            return R.error("已通过专利不可修改");
        }
        return patentMapper.updateById(patent) > 0 ? R.ok("修改成功") : R.error("修改失败");
    }
}
